
//
//  HJPasswordViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJPasswordViewController.h"
#import "HJCodeGuideController.h"
#import "HJLoginViewController.h"
#import "TZPreviewController.h"
@interface HJPasswordViewController ()<QLPreviewControllerDataSource>
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *hidAgree;
@property (weak, nonatomic) IBOutlet UILabel *hideLabel;
@property (weak, nonatomic) IBOutlet UIButton *hideAgreement;
@property (weak, nonatomic) IBOutlet UIView *superView;
@property(nonatomic,assign) BOOL agree;
@end

@implementation HJPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.superView.layer.borderWidth = 1.0;
    self.superView.layer.borderColor = HJColor(192, 204, 218).CGColor;
    self.passwordText.font = HJFontSize(mScreenWidth/414 *19);
    if (self.type==passwordType) {
        self.hidAgree.hidden = NO;
        self.hideLabel.hidden = NO;
        self.hideAgreement.hidden = NO;
    }else {
        self.hidAgree.hidden = YES;
        self.hideLabel.hidden = YES;
        self.hideAgreement.hidden = YES;
        [self.okBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    
    
    [[self.passwordText.rac_textSignal filter:^BOOL(NSString*text){
        return text.length >= 6;
    }] subscribeNext:^(NSString *text){
        self.passwordText.text = [text substringToIndex:6];
    }];
    
    
}

- (IBAction)eyeBtn:(UIButton *)sender {
    if (sender.selected) {
        self.passwordText.secureTextEntry = YES;
    }else {
        self.passwordText.secureTextEntry = NO;
    }
    sender.selected = !sender.selected;
}


- (IBAction)registerBtn:(UIButton *)sender {
    if (self.passwordText.text.length<6) {
        [self showHint:@"请输入6位数密码"];return;
    }
    if ([sender.titleLabel.text isEqualToString:@"确定"]) {
        [self modify];
    }else {
        if (self.agree ==YES) {
            [self HTTPRegister];
        }else {
            [self showHint:@"请同意用户协议"];
        }
    }
}

- (IBAction)agreeBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.agree = YES;
    }else {
        self.agree = NO;
    }
}
- (IBAction)agreeMent:(UIButton *)sender {
    TZPreviewController *previewVc = [[TZPreviewController alloc] init];
    previewVc.dataSource = self;
    previewVc.title = @"SneakerCN用户协议";
    previewVc.filename = @"用户协议.rtf";
    HJNavigationViewController * nav = [[HJNavigationViewController alloc]initWithRootViewController:previewVc];
    [self presentViewController:nav animated:YES completion:nil];
//    [self.navigationController pushViewController:previewVc animated:YES];
}


-(void)HTTPRegister {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"code"] = self.code;
    params[@"mobile"] = self.phone;
    params[@"password"] = self.passwordText.text;
    [TZHttpTool post:Register params:params success:^(NSDictionary *result) {
        [self showSuccessHUDWithStr:@"注册成功" duration:0.75];
        [HJUserManager setUserModelWithDict:result[@"data"]];
        [mUserDefaults setObject:@"1" forKey:@"isLogin"];
        [mUserDefaults setObject:self.passwordText.text forKey:@"password"];
        [mUserDefaults setObject:self.phone forKey:@"phone"];
        [mUserDefaults setObject:result[@"authToken"] forKey:@"authToken"];
//        [HJEaseManager registerWithHErrorCompletion:nil];
        [HJEaseManager loginChatClientWithCompletion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [mNotificationCenter postNotificationName:@"didRegisterSuccessNotification" object:nil];
            HJCodeGuideController *codeGuideVC = [[HJCodeGuideController alloc]init];
            [self presentViewController:codeGuideVC animated:YES completion:nil];
        });
       
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

-(void)modify {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"code"] = self.code;
    params[@"mobile"] = self.phone;
    params[@"new_password"] = self.passwordText.text;
    [TZHttpTool post:PasswordAPI params:params success:^(NSDictionary *result) {
        [self showSuccessHUDWithStr:result[@"msg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            for (UIViewController * viewC in self.navigationController.viewControllers ) {
                if ([viewC isKindOfClass:[HJLoginViewController class]]) {
                    [self.navigationController popToViewController:viewC animated:YES];
                    break;
                }
            }
        });
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
@end
